<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use fast\Tree;
use think\Db;

/**
 * 产品
 *
 * @icon fa fa-circle-o
 */
class Product extends Backend
{

    /**
     * Product模型对象
     * @var \app\admin\model\Product
     */
    protected $model = null;
    protected $relationSearch = true;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Product;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //var_dump($this->request->request("factory_id"));exit;
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //dump($where);
            $list = $this->model
                ->with(['Factory','productname'])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function indexlist()
    {
        //echo 1;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {

            $admin_type = Db::name("admin")->where(['id'=>$this->auth->id])->find();
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            //dump($where);
            $list = $this->model
                ->where($where)
                ->where(['factory_id'=>$admin_type['factory_id']])
                ->order($sort, $order)
                ->paginate($limit);
            echo $this->model->getLastSql();exit;
            $result = array("total" => $list->total(), "list" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    public function indexselect()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //var_dump($this->request->request("factory_id"));exit;
            //如果发送的来源是Selectpage，则转发到Selectpage
         /*   if ($this->request->request('keyField')) {
                return $this->selectpage();
            }*/

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            if($this->request->request("factory_id")){
                $list = $this->model
                    ->with(['Factory'])
                    ->where($where)
                    ->where(['factory_id'=>$this->request->request("factory_id")])
                    ->order($sort, $order)
                    ->paginate($limit);
            }else{
                $list = $this->model
                    ->with(['Factory'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);
            }
            //dump($where);


            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }
    /**
     * 添加
     */
    public function add()
    {
        $params = $this->request->post('row/a');
        if ( $this->request->isPost()) {
            if($this->auth->id){
                $data = Db::name("productname")->where(['id'=>$params['productname_id']])->find();
                $uin_add['name'] = $data['name'];
                $uin_add['code'] = $params['code'];
                $uin_add['producetime'] = $params['producetime'];
                $uin_add['warranty'] = $params['warranty'];
                $uin_add['productname_id'] = $params['productname_id'];
                $uin_add['createtime'] = time();
                $uin_add['factory_id'] = $params['factory_id'];
                $id = $this->model->insertGetId($uin_add);
                $f_p_data = Db::name("factory_product")->where(['factory_id'=>$params['factory_id'],'product_id'=>$params['productname_id']])->find();
                if(!$f_p_data){
                    $fp_uin_add['createtime'] = time();
                    $fp_uin_add['product_id'] = $params['productname_id'];
                    $fp_uin_add['factory_id'] = $params['factory_id'];
                     Db::name("factory_product")->insertGetId($fp_uin_add);
                }
                $this->success("添加成功");
            }else{
                $this->token();
            }

        }else{
            return parent::add();
        }
    }
    /**
     * 编辑
     */
    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);


                $f_p_data = Db::name("factory_product")->where(['factory_id'=>$params['factory_id'],'product_id'=>$params['productname_id']])->find();
                if(!$f_p_data){
                    $fp_uin_add['createtime'] = time();
                    $fp_uin_add['product_id'] = $params['productname_id'];
                    $fp_uin_add['factory_id'] = $params['factory_id'];
                    Db::name("factory_product")->insertGetId($fp_uin_add);
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }
}
